<template>
  <van-dialog
    use-slot
    title="标题"
    :show="show"
    show-cancel-button
    confirm-button-open-type="getUserInfo"
    @close="onClose"
    @getuserinfo="wxLogin"
  >
   <div class="body">
     <div class="page__bd page__bd_spacing">

             <div class="icon-box">
                 <i class="weui-icon-info weui-icon_msg"></i>

             </div>

         </div>
     <span style="width: 100%;color: #0BB20C;font-size: 36rpx;margin:20rpx 0">接受授权</span>
     <span style="width: 100%;margin-bottom: 20rpx;font-size: 22rpx">为了更好的为您提供服务,此应用需要您的授权</span>
<!--    <button-->
<!--         open-type="getUserInfo"-->
<!--          type="defult"-->
<!--          form-type="submit"-->
<!--          size="mini"-->
<!--          @getuserinfo="wxLogin"-->

<!--           style="width: 80%;background: #0BB20C;color: #fff;">授权登录</button>-->
   </div>
  </van-dialog>
</template>

<script>
  export default{
    name: 'getUserinfo',
    props: ['isShow'],
    data () {
      return {
        show: false,
        empId:"",
        num:""
      }
    },
    onShow(){

    },
    methods: {
      init(empId,num){
       // console.log(empId)
        this.num=num;
        this.empId=empId;
        this.show=true;
      },
      wxLogin (e) {
        let that = this
        //console.log(e)
        wx.login({
          success (res) {
          //  console.log(that.empId)
            that.code = res.code
            if (e.mp.detail.userInfo) {
              that.$http.request({
                method: 'post',
                url: '/api/wxuser/login',
                body: {
                  code: res.code,
                  empId:that.empId,
                  encryptedData: e.mp.detail.encryptedData,
                  iv: e.mp.detail.iv,
                  rawData: e.mp.detail.rawData,
                  signature: e.mp.detail.signature
                }
              }).then((data) => {
             //   console.log(data)
                if (data.code == 200) {

                  // if(that.empId){

                  // }
                  wx.setStorageSync('Authorization', data.data.token)
                   // wx.setStorageSync('Authorization1', data.data.token)
                    wx.setStorageSync('userId',  data.data.user.id)
                    // wx.setStorageSync('userId1',  data.data.user.id)
                  wx.setStorageSync('cartId',  data.data.user.cardId)
                  // wx.setStorageSync('cartId1',  data.data.user.cardId)
                     wx.setStorageSync('isVip', data.data.user.isvip)
                    wx.setStorageSync('openId',data.data.user.openid)
                    // if(data.data.user.cardId){
                    //     that.loginGetCartDetail(that,data.data.user,data.data.user.cardId)
                    // }
                  wx.setStorageSync('userInfo', JSON.stringify(e.mp.detail.rawData))
                  //  that.getcardForward(data.data.user.id)
                  if(that.num){
                    that.$emit('changeDom',data.data.user)
                  }

                  // wx.showToast({
                  //   title: '登录成功!',
                  //   icon: 'success',
                  //   duration: 2000,
                  //   success: () => {
                  //
                  //
                  //       console.log(JSON.stringify(e.mp.detail.rawData))
                  //
                  //
                  //
                  //     // wx.navigateTo({ url: '/pages/index/main' })
                  //   }
                  // })
                } else {
                  wx.showToast({
                    title: '登录失败,请重新登陆!',
                    icon: 'none',
                    duration: 2000

                  })
                }
              })
            } else {

            }
            //    wx.setStorageSync('openid',  that.code)
            // that.setAppletCode(res.code)
            // that.wxGetUserInfo(res.code)
          }
        })
      },
        loginGetCartDetail(that,userId,cartId){

            that.$http.request({
                method: 'get',
                url: `/api/card/detail`,
                body: {
                  cardId:cartId||'',
                  userId:userId.id||''
                }
            }).then((data) => {
                if (data.code == 200) {
                    wx.setStorageSync('cartId', data.data.id)
                    wx.setStorageSync('cartId1', data.data.id)
                    wx.setStorageSync('isVip', data.data.isVip)
                    let obj={};
                    obj.logo=data.data.logo;
                    obj.name=data.data.name;
                    obj.company=data.data.company;
                    wx.setStorageSync('shareData', JSON.stringify(obj));
                } else {

                    wx.showToast({
                        title: `${data.message}` ,
                        icon: 'none',
                        duration: 2000
                    })
                }
            })
        },
      getcardForward(userId){//添加浏览记录
        let cartId=wx.getStorageSync('cartId')
          this.$http.request({
            method:"post",
            url:"/api/cardForward/add",
            body:{
              "cardId": cartId,


              // "empId":this.empId,
              "forwarderId":userId,

              "type": 2,
              "userId":  this.empId
            }
          }).then((data)=>{
            if(data.code==200){
                // if(!that.num){
                  this.$emit('getBtnType',data.data.btnStatus)
                // }
                wx.showToast({
                  title: '添加成功',
                  icon: 'success',
                  duration: 2000

                })
            }
          })
      },
      wxGetUserInfo (code) {
        const that = this
        wx.getUserInfo({
          withCredentials: true,
          success (res) {
            console.log(res)
          },
          fail (err) {
            console.log('自动wx.getUserInfo失败:', err)
            // 显示主动授权的button
            that.buttonVisible = true
          }
        })
      },
      getUserInfo (event) {
        console.log(event.detail)
      },
      onClose () {
        this.show = false
      }
    }

  }
</script>

<style scoped>
  .body{
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding-top:30rpx;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
  }
</style>
